﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;
using System.Web.Security;

namespace GameSchool.Controllers
{
    public class AchievementController : Controller
    {
        //
        // GET: /Achievement/
        GameSchoolRepository repository = new GameSchoolRepository();
        #region Views

        //For students
        public ActionResult XPAchievement(int CID)
        {
            var viewmodel = new XPAchievementViewModel();
            var studentLogin = repository.GetStudentLoginRelationship();

 
            viewmodel.listAchievements = repository.GetAchievements().ToList();
            viewmodel.studentName = studentLogin.name; //For the top bar
            viewmodel.sidebarCourses = repository.GetCoursesByStudent(studentLogin).ToList();
            viewmodel.listCourses = repository.GetCoursesByStudent(studentLogin).ToList();
            GameSchoolContext db = new GameSchoolContext();

            return View(viewmodel);
        }

        //For teachers
        public ActionResult CreateAchievement(int CID)
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateAchievement(FormCollection formData)
        {
            return View();
        }

        #endregion

        public Notification GetNotifications(Notification theNotification)
        {
            return theNotification;
        }

        /// <summary>
        /// Creates an achievement related specifically to the course that was created, 
        /// e.g. "completed Forritun!".
        /// </summary>
        /// <param name="theCourse"></param>
        /// <returns></returns>
        public void CreateAchievementForCourse(Course theCourse)
        {
            Achievement courseAchievement = new Achievement
            {
                title = "Completed " + theCourse.title,
                description = "You completed the course " + theCourse.title + ".",
                type = "1"
            };

            repository.AddAchievement(courseAchievement);
            repository.Save();
        }

        public void CreateAchievementForLevel(Level theLevel)
        {
            var courseInLevel = repository.GetCourseByLevel(theLevel);
            Achievement levelAchievement = new Achievement
            {
                title = "Completed level " + theLevel.number + " in " + courseInLevel.title + ".",
                description = "You completed level " + theLevel.number + " in " + courseInLevel + "!",
                type = "2"
            };

            repository.AddAchievement(levelAchievement);
            repository.Save();
        }

		public ActionResult AssignAchievement(int CID)
		{
			var viewmodel = new TeacherCourseViewModel();

			var teacherLogin = repository.GetTeacherLoginRelationship();

			var theCourse = repository.GetCourseByID(CID);

			viewmodel.levels = repository.GetAllLevelsByCourse(theCourse); //getting levels for a particular course.
			viewmodel.sidebarCourses = repository.GetCoursesByTeacher(teacherLogin).ToList(); //for sidebar.
			viewmodel.teacherName = teacherLogin.name; //for titlebar.
			viewmodel.listCourses = repository.GetCoursesByTeacher(teacherLogin).ToList();
			viewmodel.courseName = theCourse.title;
			viewmodel.courseID = theCourse.courseID;

			return View(viewmodel);
		}
    }
}
